<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>神秘竞拍网站</title>
  <style>
    body {
      font-family: 'Roboto Mono', monospace;
      background-color: #000;
      color: #888;
      margin: 0;
      padding: 0;
      overflow: hidden;
    }

   .container {
      width: 500px;
      margin: 100px auto;
      padding: 30px;
      border: 1px solid #333;
      border-radius: 10px;
      box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
      background-color: rgba(0, 0, 0, 0.7);
    }

    h2 {
      color: #00ff99;
      border-bottom: 1px solid #333;
      padding-bottom: 10px;
      margin-bottom: 20px;
    }

    input {
      width: 100%;
      padding: 15px;
      margin-bottom: 20px;
      box-sizing: border-box;
      background-color: #111;
      color: #fff;
      border: 1px solid #333;
      border-radius: 5px;
    }

    button {
      width: 100%;
      padding: 15px;
      background-color: #007BFF;
      color: #fff;
      border: none;
      border-radius: 5px;
      cursor: pointer;
      transition: background-color 0.3s ease;
    }

    button:hover {
      background-color: #0056b3;
    }

   .loading-overlay {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.8);
      display: flex;
      justify-content: center;
      align-items: center;
      z-index: 1000;
      display: none;
    }

   .loading-spinner {
      width: 40px;
      height: 40px;
      border: 4px solid rgba(255, 255, 255, 0.1);
      border-top: 4px solid #fff;
      border-radius: 50%;
      animation: spin 1s linear infinite;
    }

    @keyframes spin {
      0% {
        transform: rotate(0deg);
      }

      100% {
        transform: rotate(360deg);
      }
    }
  </style>
</head>

<body>
  <div class="container">
    <h2>用户竞拍</h2>
    <input type="number" id="bidAmount" placeholder="输入竞拍金额">
    <button onclick="placeBid()">竞拍</button>

    <h2>用户提款</h2>
    <button onclick="withdrawFunds()">提款</button>

    <h2>结束拍卖</h2>
    <button onclick="endAuction()">结束拍卖</button>
  </div>
  <div class="loading-overlay">
    <div class="loading-spinner"></div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/web3@1.7.4/dist/web3.min.js"></script>
  <script>
    let web3;
    let accounts = [];
    let contract;

    // 合约地址和 ABI，需要替换为你自己的合约信息
    const contractAddress = 'YOUR_CONTRACT_ADDRESS';
    const contractABI = [
      // 这里需要替换为你的合约 ABI
    ];

    async function connectMetamask() {
      if (window.ethereum) {
        try {
          accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
          web3 = new Web3(window.ethereum);
          contract = new web3.eth.Contract(contractABI, contractAddress);
          console.log('已连接到 MetaMask，账户:', accounts[0]);
        } catch (error) {
          console.error('用户拒绝授权或发生错误:', error);
          alert('用户拒绝授权或发生错误，请重试。');
        }
      } else {
        alert('请安装 MetaMask 钱包以使用此功能。');
      }
    }

    function showLoading() {
      const overlay = document.querySelector('.loading-overlay');
      overlay.style.display = 'flex';
    }

    function hideLoading() {
      const overlay = document.querySelector('.loading-overlay');
      overlay.style.display = 'none';
    }

    async function placeBid() {
      if (accounts.length === 0) {
        await connectMetamask();
        if (accounts.length === 0) return;
      }
      const bidAmount = document.getElementById('bidAmount').value;
      if (bidAmount) {
        showLoading();
        try {
          // 调用智能合约的竞拍方法
          const result = await contract.methods.placeBid().send({
            from: accounts[0],
            value: web3.utils.toWei(bidAmount, 'ether')
          });
          alert('竞拍成功，交易哈希: ' + result.transactionHash);
        } catch (error) {
          console.error('竞拍失败:', error);
          alert('竞拍失败，请检查网络或重试。');
        } finally {
          hideLoading();
        }
      } else {
        alert('请输入竞拍金额');
      }
    }

    async function withdrawFunds() {
      if (accounts.length === 0) {
        await connectMetamask();
        if (accounts.length === 0) return;
      }
      showLoading();
      try {
        // 调用智能合约的提款方法
        const result = await contract.methods.withdrawFunds().send({
          from: accounts[0]
        });
        alert('提款成功，交易哈希: ' + result.transactionHash);
      } catch (error) {
        console.error('提款失败:', error);
        alert('提款失败，请检查网络或重试。');
      } finally {
        hideLoading();
      }
    }

    async function endAuction() {
      if (accounts.length === 0) {
        await connectMetamask();
        if (accounts.length === 0) return;
      }
      showLoading();
      try {
        // 调用智能合约的结束拍卖方法
        const result = await contract.methods.endAuction().send({
          from: accounts[0]
        });
        alert('拍卖已结束，交易哈希: ' + result.transactionHash);
      } catch (error) {
        console.error('结束拍卖失败:', error);
        alert('结束拍卖失败，请检查网络或重试。');
      } finally {
        hideLoading();
      }
    }
  </script>
</body>

</html>    